<?php 


	fta_registerModules(
		array(
			"FTAORM/FTAORM",
		)
	);
	
class FTAORMCMSBinaryData extends FTAORM  {

	const TABLE_SEQUENCE_NAME = 'binarydatatablesequence_seq';
	function __construct($zdb) {
		
		parent::__construct($zdb);			
			$this->primaryKey = "id";
			$this->tableFields = array(
			  'documentcode' ,
			  'elementid' ,
			  'filetypecode' ,
			  'content' ,
			  'filesize' ,
			  'mimetype' ,
			  'filename' 			
			);//excluding the pk..
									
			$this->tableFieldTypes = array(
			  'documentcode' => 'number' ,
			  'elementid' => 'number',
			  'filetypecode' => 'number',
			  'content' =>'value',
			  'filesize' => 'number',
			  'mimetype' => 'string',
			  'filename' => 'string' 			
			);
						
			$this->tableRequiredFields = array(
				'documentcode',
				'elementid'				
			);
			
			$this->tableName = "binarydata";	
			
			$this->tableRelationships = array();

			$this->tableRelationshipClasses = array();	
	
		}	


		/**
		 * Gets all binarydata for the given document code .. primarily used for cloning
		 * @param unknown_type $documentCode
		 * @param unknown_type $zdb
		 */
		static function getAllBinaryDataForDocumentCode($documentCode, $zdb){
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(\Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName))
                ->where('documentcode = ?', $documentCode);
                            	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);			
		}
				
		static function getNewPrimaryKey(Zend_Db_Adapter_Abstract $zdb){			
			$zdb->query('UPDATE ' . static::TABLE_SEQUENCE_NAME .' SET id=LAST_INSERT_ID(id+1)');
			$id = $zdb->lastInsertId();
			return $id;
		}			
}